package com.airtight.screens;

import android.content.Context;
import android.util.Log;
import android.view.MotionEvent;

import com.airtight.game.GameState;
import com.airtight.interfaces.Screen;
import com.airtight.objects.Background;
import com.airtight.objects.ClickableObject;
import com.airtight.programs.ColorShaderProgram;
import com.airtight.programs.TextureShaderProgram;
import com.example.android.opengl.R;

import static java.lang.Math.abs;

public class MainMenu extends Screen{


    private Context context;
    private ScreenManager screenManager;
    private GameState gameState;
    public Background background;
    private ClickableObject[] clickObjects = new ClickableObject[4];

    private TextureShaderProgram textureProgram;
    private ColorShaderProgram colorProgram;

    private float ratio;

    MainMenu(Context context, ScreenManager screenManager, GameState gameState){
        this.context = context;
        this.screenManager = screenManager;
        this.gameState = gameState;
        this.background = gameState.objects.background;
        System.arraycopy(gameState.objects.clickObjects, 0, this.clickObjects, 0, 4);
        textureProgram = new TextureShaderProgram(context, R.raw.texture_vertex_shader, R.raw.texture_fragment_shader);
        colorProgram = new ColorShaderProgram(context);
        //Log.d("create surface ","menu");
    }

    public void changeSurface(float ratio) {
        screenManager.setRatio(ratio);
        this.ratio = ratio;

        //Log.d("change surface ","menu");
    }

    public void update() {

    }

    public void draw() {
        background.drawBackground(textureProgram,screenManager.getAssets().background_texture);
        for(int i = 0; i<4;i++){
            clickObjects[i].drawObject(textureProgram,screenManager.getAssets().play_plate);
        }
        //Log.d("drawing","frame");
    }

    public void pause() {

    }

    public void resume() {

    }

    public void dispose() {

    }

    public void onBackPressed() {

    }

    public void TouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                /*for(int i = 0; i<4;i++){
                    clickObjects[i] = new ClickableObject(0, 0.2f - 0.25f*i,0.5f,0.2f/ratio);
                }*/


                float x =  (2*event.getX()-gameState.width)/gameState.width;
                Log.d("X",""+x);
                if(abs(x) < 0.5f){
                    float y = (-2*event.getY() + gameState.height)/gameState.height;
                    Log.d("Y", ""+y);
                    if(y > 0.2f - 0.2f/ratio && y < 0.2f + 0.2f/ratio){
                    //if(y>0.2f){
                        screenManager.setWorld();
                    }
                }
                break;

        }
    }

    public boolean getState() {
        return false;
    }

    public String toString() {
        return "MainMenu";
    }
}
